// pages/user/empower/empowerList/empowerList.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        applyType: ['', '金牌店主', '服务商'],
        empowerList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getEmpowerList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getEmpowerList()
    },

    getEmpowerList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let empowerList = this.data.empowerList
        let data = {}
        common.post('Quota/apply_role_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    empowerList: empowerList,
                    isAll: true
                })
            } else {
                empowerList = empowerList.concat(res)
                that.setData({
                    empowerList: empowerList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    agree: function(e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认通过该用户申请',
            success(res) {
                if (res.confirm) {
                    let data = {
                        quota_apply_id: e.currentTarget.dataset.id,
                        type: 1
                    }
                    common.post('Quota/apply_role_check', data).then(function(res) {
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                        } else {
                            wx.showModal({
                                title: '提示',
                                content: '通过成功',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        that.setData({
                                            empowerList: [],
                                            nowPage: 0,
                                            isLoading: false,
                                            isAll: false
                                        })
                                        that.getEmpowerList()
                                    }
                                }
                            })
                        }
                    }).catch(function(e) {})
                }
            }
        })
    },

    noAgree: function(e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认拒绝该用户申请',
            success(res) {
                if (res.confirm) {
                    let data = {
                        quota_apply_id: e.currentTarget.dataset.id,
                        type: 2
                    }
                    common.post('Quota/apply_role_check', data).then(function(res) {
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                        } else {
                            wx.showModal({
                                title: '提示',
                                content: '拒绝成功',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        that.setData({
                                            empowerList: [],
                                            nowPage: 0,
                                            isLoading: false,
                                            isAll: false
                                        })
                                        that.getEmpowerList()
                                    }
                                }
                            })
                        }
                    }).catch(function(e) {})
                }
            }
        })
    },

    phoneCall: function(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        })
    }
})